package com.mashrabenaab.adapter;

import java.util.ArrayList;

import com.mashrabenaab.GlobalService;
import com.mashrabenaab.Magazine;
import com.mashrabenaab.R;
import com.mashrabenaab.R.id;
import com.mashrabenaab.R.layout;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MnAdapterLive extends BaseAdapter {
	public int count = 10;
	private Activity activity;
	private ArrayList<Magazine> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public MnAdapterLive(Activity a, ArrayList<Magazine> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return data.get(position);
	}

	public int getPosition(String value) {
		return data.indexOf(value);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.mn_row_live, null);
		
		Magazine item = data.get(position);
		
		
		TextView textEdition = (TextView) vi
				.findViewById(R.id.mn_row_text_edition);
		if (textEdition != null) {
			if (item != null) {
				textEdition.setText(item.getEdition());
			} else {
				textEdition.setText("No Edition");
			}
		}

		TextView textMonthName = (TextView) vi
				.findViewById(R.id.mn_row_text_monthname);
		if (textMonthName != null) {
			if (item != null) {
				textMonthName.setText(GlobalService.getMonthName(item.getMonthName()));
			} else {
				textMonthName.setText("Not Available");
			}
		}
		
		TextView textYearName = (TextView) vi
				.findViewById(R.id.mn_row_text_yearname);
		if (textYearName != null) {
			if (item != null) {
				textYearName.setText(item.getYearName());
			} else {
				textYearName.setText("Not Available");
			}
		}

		
		ImageView image = (ImageView) vi.findViewById(R.id.mn_row_image);
		if (image != null) {
			if (item != null) {
				imageLoader.DisplayImage(item.getImageUrl(), activity, image);
			} else {
			}
		}
		return vi;
	}

}
